package com.gun.management.module.raffle.controller.admin.lotteryrecord;

import com.gun.management.framework.security.core.LoginUser;
import com.gun.management.module.raffle.controller.admin.lotterylog.vo.LotteryLogPageReqVO;
import com.gun.management.module.raffle.controller.admin.lotterylog.vo.LotteryLogRespVO;
import com.gun.management.module.raffle.dal.dataobject.lotteryrule.LotteryRuleDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.IOException;

import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import static com.gun.management.framework.common.pojo.CommonResult.success;
import static com.gun.management.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

import com.gun.management.framework.excel.core.util.ExcelUtils;

import com.gun.management.module.raffle.controller.admin.lotteryrecord.vo.*;
import com.gun.management.module.raffle.dal.dataobject.lotteryrecord.LotteryRecordDO;
import com.gun.management.module.raffle.service.lotteryrecord.LotteryRecordService;

@Tag(name = "管理后台 - 用户抽奖履历")
@RestController
@RequestMapping("/raffle/lotteryrecord")
@Validated
public class LotteryRecordController {

    @Resource
    private LotteryRecordService lotteryRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户抽奖履历")
    @PreAuthorize("@ss.hasPermission('raffle:lottery-record:create')")
    public CommonResult<Long> createLotteryRecord(@Valid @RequestBody LotteryRecordSaveReqVO createReqVO) {
        return success(lotteryRecordService.createLotteryRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户抽奖履历")
    @PreAuthorize("@ss.hasPermission('raffle:lottery-record:update')")
    public CommonResult<Boolean> updateLotteryRecord(@Valid @RequestBody LotteryRecordSaveReqVO updateReqVO) {
        lotteryRecordService.updateLotteryRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户抽奖履历")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('raffle:lottery-record:delete')")
    public CommonResult<Boolean> deleteLotteryRecord(@RequestParam("id") Long id) {
        lotteryRecordService.deleteLotteryRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户抽奖履历")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('raffle:lottery-record:query')")
    public CommonResult<LotteryRecordRespVO> getLotteryRecord(@RequestParam("id") Long id) {
        LotteryRecordDO lotteryRecord = lotteryRecordService.getLotteryRecord(id);
        return success(BeanUtils.toBean(lotteryRecord, LotteryRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户抽奖履历分页")
    @PreAuthorize("@ss.hasPermission('raffle:lotteryrecord:page')")
    public CommonResult<PageResult<LotteryRecordRespVO>> getLotteryRecordPage(@Valid LotteryRecordReqVO reqVO) {
        LoginUser loginUser = getLoginUser();
        PageResult<LotteryRecordRespVO> pageResult = lotteryRecordService.getLotteryRecordPage(loginUser.getId(),loginUser.getTenantId(),reqVO);
        return success(pageResult);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出用户抽奖履历 Excel")
//    @PreAuthorize("@ss.hasPermission('raffle:lottery-record:export')")
//
//    public void exportLotteryRecordExcel(@Valid LotteryRecordPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<LotteryRecordDO> list = lotteryRecordService.getLotteryRecordPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "用户抽奖履历.xls", "数据", LotteryRecordRespVO.class,
//                        BeanUtils.toBean(list, LotteryRecordRespVO.class));
//    }

}